import dotenv from 'dotenv';

// 加载环境变量
dotenv.config();

export const config = {
  // 服务器配置
  port: process.env['PORT'] || 3000,
  nodeEnv: process.env['NODE_ENV'] || 'development',

  // MongoDB配置
  mongodbUri:
    process.env['MONGODB_URI'] || 'mongodb://localhost:27017/autocost',

  // JWT配置
  jwtSecret: process.env['JWT_SECRET'] || 'your-jwt-secret-key',
  jwtExpiresIn: process.env['JWT_EXPIRES_IN'] || '7d',

  // 文件上传配置
  uploadPath: process.env['UPLOAD_PATH'] || './uploads',
  maxFileSize: parseInt(process.env['MAX_FILE_SIZE'] || '5242880'),

  // 阿里云OSS配置（可选）
  ossAccessKeyId: process.env['OSS_ACCESS_KEY_ID'],
  ossAccessKeySecret: process.env['OSS_ACCESS_KEY_SECRET'],
  ossBucket: process.env['OSS_BUCKET'],
  ossRegion: process.env['OSS_REGION'] || 'oss-cn-hangzhou',

  // 日志配置
  logLevel: process.env['LOG_LEVEL'] || 'info',
  logFile: process.env['LOG_FILE'] || './logs/app.log',

  // 安全配置
  bcryptRounds: parseInt(process.env['BCRYPT_ROUNDS'] || '12'),
  rateLimitWindowMs: parseInt(process.env['RATE_LIMIT_WINDOW_MS'] || '900000'),
  rateLimitMaxRequests: parseInt(
    process.env['RATE_LIMIT_MAX_REQUESTS'] || '100'
  ),

  // 跨域配置
  corsOrigin: process.env['CORS_ORIGIN'] || 'http://localhost:3000',
};

// 验证必要的环境变量
export const validateConfig = (): void => {
  const requiredEnvVars = ['JWT_SECRET'];

  for (const envVar of requiredEnvVars) {
    if (!process.env[envVar]) {
      console.warn(`⚠️  Warning: ${envVar} is not set, using default value`);
    }
  }
};

// 检查是否为生产环境
export const isProduction = (): boolean => {
  return config.nodeEnv === 'production';
};

// 检查是否为开发环境
export const isDevelopment = (): boolean => {
  return config.nodeEnv === 'development';
};
